package xxm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import xxm.exception.DAOException;
import xxm.model.CateInfo;
import xxm.util.DB;

public class CateDaoImpl implements xxm.dao.ICateDao {

	private Connection con = null;
	private PreparedStatement ps=null;
	private ResultSet rs = null;
	
	@Override
	public boolean delete(CateInfo e) throws DAOException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(int id) throws DAOException {
		// TODO Auto-generated method stub
		try {
			String sql = "DELETE FROM xxm_marketing.tb_market_cate_info WHERE market_cate_id = ?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, id);
			int result = ps.executeUpdate();
			if (result == 1) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int getCount() throws DAOException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public CateInfo loadById(int id) throws DAOException {
		// TODO Auto-generated method stub
		CateInfo catoInfo = null;
		try {
			String sql = "SELECT market_cate_id,market_cate_parent_id,market_cate_rank," +
			"market_cate_name, market_cate_alias, market_cate_desc," +
			"market_cate_status, market_cate_date FROM tb_market_cate_info WHERE market_cate_id=?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				catoInfo = new CateInfo();
				catoInfo.setId(rs.getInt("market_cate_id"));
				catoInfo.setParentId(rs.getInt("market_cate_parent_id"));
				catoInfo.setName(rs.getString("market_cate_name"));
				catoInfo.setAlias(rs.getString("market_cate_alias"));
				catoInfo.setRank(rs.getInt("market_cate_rank"));
				catoInfo.setDesc(rs.getString("market_cate_desc"));
				catoInfo.setStatus(rs.getInt("market_cate_status"));
				catoInfo.setDate(rs.getDate("market_cate_date"));
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DAOException(e);
		}
		
		return catoInfo;
	}

	@Override
	public List<CateInfo> paging(int pageSize, int pageCount, int rowCount)
			throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean save(CateInfo cateInfo) throws DAOException {
		// TODO Auto-generated method stub
		try {
			String sql = "INSERT INTO xxm_marketing.tb_market_cate_info "+
			"(market_cate_parent_id, "+
			"market_cate_rank, "+
			"market_cate_name, "+
			"market_cate_alias, "+
			"market_cate_desc, "+
			"market_cate_status)"+
			" VALUES(?,0,?,?,?,0)";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, cateInfo.getParentId());
			ps.setString(2, cateInfo.getName());
			ps.setString(3, cateInfo.getAlias());
			ps.setString(4, cateInfo.getDesc());
			int result = ps.executeUpdate();
			if (result == 1) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DAOException(e);
		}
		return false;
	}

	@Override
	public boolean update(CateInfo cateInfo) throws DAOException {
		// TODO Auto-generated method stub
		try {
			String sql = "UPDATE tb_market_cate_info "+
			"SET "+
			//"market_cate_id = 'market_cate_id' , "+
			"market_cate_parent_id = ? , "+
			//"market_cate_rank = 'market_cate_rank' , "+
			"market_cate_name = ? , "+
			"market_cate_alias = ? , "+
			"market_cate_desc = ? "+
			//"market_cate_status = 'market_cate_status' , "+
			//"market_cate_date = 'market_cate_date'"+
		
			"WHERE "+
			"market_cate_id = ?";
			
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, cateInfo.getParentId());
			ps.setString(2, cateInfo.getName());
			ps.setString(3, cateInfo.getAlias());
			ps.setString(4, cateInfo.getDesc());
			ps.setInt(5, cateInfo.getId());
			int result = ps.executeUpdate();
			if (result == 1) {
				return true;
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return false;
	}

	@Override
	public List<CateInfo> getTableArray() throws DAOException {
		List<CateInfo> ListArray = new ArrayList<CateInfo>();
		try {
			String sql = "SELECT market_cate_id,market_cate_parent_id,market_cate_rank," +
			"market_cate_name, market_cate_alias, market_cate_desc," +
			"market_cate_status, market_cate_date FROM tb_market_cate_info";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				CateInfo catoInfo = new CateInfo();
				catoInfo.setId(rs.getInt("market_cate_id"));
				catoInfo.setParentId(rs.getInt("market_cate_parent_id"));
				catoInfo.setName(rs.getString("market_cate_name"));
				catoInfo.setAlias(rs.getString("market_cate_alias"));
				catoInfo.setRank(rs.getInt("market_cate_rank"));
				catoInfo.setDesc(rs.getString("market_cate_desc"));
				catoInfo.setStatus(rs.getInt("market_cate_status"));
				catoInfo.setDate(rs.getDate("market_cate_date"));
				ListArray.add(catoInfo);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return ListArray;
	}

	@Override
	public List<CateInfo> getTableVectorByName(String strKey) throws DAOException {
		// TODO Auto-generated method stub
		Vector<CateInfo> vector = new Vector<CateInfo>();
		try {
			String sql = "SELECT market_cate_id,market_cate_parent_id,market_cate_rank," +
			"market_cate_name, market_cate_alias, market_cate_desc," +
			"market_cate_status, market_cate_date FROM tb_market_cate_info where market_cate_name like ?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setString(1, "%"+strKey+"%");
			rs = ps.executeQuery();
			while (rs.next()) {
				/*Vector<String> v= new Vector<String>();
				v.add(rs.getString("market_cate_id"));
				v.add(rs.getString("market_cate_parent_id"));
				v.add(rs.getString("market_cate_name"));
				v.add(rs.getString("market_cate_desc"));
				v.add(rs.getString("market_cate_status"));
				v.add(rs.getString("market_cate_date"));
				vector.add(v);*/
				CateInfo catoInfo = new CateInfo();
				catoInfo.setId(rs.getInt("market_cate_id"));
				catoInfo.setParentId(rs.getInt("market_cate_parent_id"));
				catoInfo.setName(rs.getString("market_cate_name"));
				catoInfo.setAlias(rs.getString("market_cate_alias"));
				catoInfo.setRank(rs.getInt("market_cate_rank"));
				catoInfo.setDesc(rs.getString("market_cate_desc"));
				catoInfo.setStatus(rs.getInt("market_cate_status"));
				catoInfo.setDate(rs.getDate("market_cate_date"));
				vector.add(catoInfo);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return vector;
	}

	@Override
	public List<CateInfo> getTableVectorByCode(String strKey) throws DAOException {
		// TODO Auto-generated method stub
		//Vector<Vector<String>> vector = new Vector<Vector<String>>();
		Vector<CateInfo> vector = new Vector<CateInfo>();
		try {
			String sql = "SELECT market_cate_id,market_cate_parent_id,market_cate_rank," +
			"market_cate_name, market_cate_alias, market_cate_desc," +
			"market_cate_status, market_cate_date FROM tb_market_cate_info where market_cate_id like ?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setString(1, "%"+strKey+"%");
			rs = ps.executeQuery();
			while (rs.next()) {
				/*Vector<String> v= new Vector<String>();
				v.add(rs.getString("market_cate_id"));
				v.add(rs.getString("market_cate_parent_id"));
				v.add(rs.getString("market_cate_name"));
				v.add(rs.getString("market_cate_desc"));
				v.add(rs.getString("market_cate_status"));
				v.add(rs.getString("market_cate_date"));
				vector.add(v);*/
				CateInfo catoInfo = new CateInfo();
				catoInfo.setId(rs.getInt("market_cate_id"));
				catoInfo.setParentId(rs.getInt("market_cate_parent_id"));
				catoInfo.setName(rs.getString("market_cate_name"));
				catoInfo.setAlias(rs.getString("market_cate_alias"));
				catoInfo.setRank(rs.getInt("market_cate_rank"));
				catoInfo.setDesc(rs.getString("market_cate_desc"));
				catoInfo.setStatus(rs.getInt("market_cate_status"));
				catoInfo.setDate(rs.getDate("market_cate_date"));
				vector.add(catoInfo);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return vector;
	}

}
